<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends Model {
  public $name = 'User';
  /*
  public $belongsTo = array(
    'Group' => array(
      'className' => 'Group',
      'foreignKey' => 'group_id'   
      )
    );
   */
  public $belongsTo = array('Group');
  public $actsAs = array('Acl' => array('type' => 'requester'));
  public $validate = array(
    'username' => array(
      'unique' => array(
          'rule' => 'isUnique',
          'message' => 'username must be unique'
        )
      )
    );

  public function beforeSave() {
    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    return true;
  }

  public function parentNode() {
    if (!$this->id && empty($this->data)) {
      return null;
    }
    if (isset($this->data['User']['group_id'])) {
      $groupId = $this->data['User']['group_id'];
    } else {
      $groupId = $this->field('group_id');
    }
    if (!$groupId) {
      return null;
    } else {
      return array('Group' => array('id' => $groupId));
    }
  }
  public function bindNode($user) {
    return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
  }
}
?>
